{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "24545b0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGN_CNT_RCT_12_MON</th>\n",
       "      <th>ICO_CUR_MON_ACM_TRX_TM</th>\n",
       "      <th>NB_RCT_3_MON_LGN_TMS_AGV</th>\n",
       "      <th>AGN_CUR_YEAR_AMT</th>\n",
       "      <th>AGN_CUR_YEAR_WAG_AMT</th>\n",
       "      <th>AGN_AGR_LATEST_AGN_AMT</th>\n",
       "      <th>ICO_CUR_MON_ACM_TRX_AMT</th>\n",
       "      <th>COUNTER_CUR_YEAR_CNT_AMT</th>\n",
       "      <th>PUB_TO_PRV_TRX_AMT_CUR_YEAR</th>\n",
       "      <th>MON_12_EXT_SAM_TRSF_IN_AMT</th>\n",
       "      <th>...</th>\n",
       "      <th>WTHR_OPN_ONL_ICO</th>\n",
       "      <th>EMP_NBR</th>\n",
       "      <th>REG_CPT</th>\n",
       "      <th>SHH_BCK</th>\n",
       "      <th>HLD_DMS_CCY_ACT_NBR</th>\n",
       "      <th>REG_DT</th>\n",
       "      <th>LGP_HLD_CARD_LVL</th>\n",
       "      <th>OPN_TM</th>\n",
       "      <th>NB_CTC_HLD_IDV_AIO_CARD_SITU</th>\n",
       "      <th>HLD_FGN_CCY_ACT_NBR</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CUST_UID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>deca9c4409e84344bc4059116902ba0e</th>\n",
       "      <td>23662.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>135692.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>594825002.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>26622.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>242461.35</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1588.13</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25377f79cf9d4964a2f9893dc572caba</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>B</td>\n",
       "      <td>1502.0</td>\n",
       "      <td>3.800000e+08</td>\n",
       "      <td>32.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>432.00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>82.65</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66979a1614fb49f1bcdcdce75d6abefc</th>\n",
       "      <td>1672.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>48.7</td>\n",
       "      <td>992125.6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>992125.6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>512.65</td>\n",
       "      <td>C</td>\n",
       "      <td>22.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84142dedb7e0442eb9aa7aeadf93f66e</th>\n",
       "      <td>5432.0</td>\n",
       "      <td>102.0</td>\n",
       "      <td>88.7</td>\n",
       "      <td>46729796.3</td>\n",
       "      <td>46729796.3</td>\n",
       "      <td>7356752.4</td>\n",
       "      <td>8048976.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2154106.4</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>B</td>\n",
       "      <td>432.0</td>\n",
       "      <td>5.000002e+06</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>442.00</td>\n",
       "      <td>C</td>\n",
       "      <td>432.00</td>\n",
       "      <td>C</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5e5817f15c8d4bdebefa820961854ac2</th>\n",
       "      <td>11842.0</td>\n",
       "      <td>782.0</td>\n",
       "      <td>1545.3</td>\n",
       "      <td>27085791.9</td>\n",
       "      <td>NaN</td>\n",
       "      <td>135002.0</td>\n",
       "      <td>81516420.9</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2421499.6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>B</td>\n",
       "      <td>902.0</td>\n",
       "      <td>1.131689e+09</td>\n",
       "      <td>32.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>3747.48</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3200.06</td>\n",
       "      <td>C</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  AGN_CNT_RCT_12_MON  ICO_CUR_MON_ACM_TRX_TM  \\\n",
       "CUST_UID                                                                       \n",
       "deca9c4409e84344bc4059116902ba0e             23662.0                     NaN   \n",
       "25377f79cf9d4964a2f9893dc572caba                 NaN                     2.0   \n",
       "66979a1614fb49f1bcdcdce75d6abefc              1672.0                     2.0   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e              5432.0                   102.0   \n",
       "5e5817f15c8d4bdebefa820961854ac2             11842.0                   782.0   \n",
       "\n",
       "                                  NB_RCT_3_MON_LGN_TMS_AGV  AGN_CUR_YEAR_AMT  \\\n",
       "CUST_UID                                                                       \n",
       "deca9c4409e84344bc4059116902ba0e                       NaN               NaN   \n",
       "25377f79cf9d4964a2f9893dc572caba                       NaN               NaN   \n",
       "66979a1614fb49f1bcdcdce75d6abefc                      48.7          992125.6   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e                      88.7        46729796.3   \n",
       "5e5817f15c8d4bdebefa820961854ac2                    1545.3        27085791.9   \n",
       "\n",
       "                                  AGN_CUR_YEAR_WAG_AMT  \\\n",
       "CUST_UID                                                 \n",
       "deca9c4409e84344bc4059116902ba0e                   NaN   \n",
       "25377f79cf9d4964a2f9893dc572caba                   NaN   \n",
       "66979a1614fb49f1bcdcdce75d6abefc                   NaN   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e            46729796.3   \n",
       "5e5817f15c8d4bdebefa820961854ac2                   NaN   \n",
       "\n",
       "                                  AGN_AGR_LATEST_AGN_AMT  \\\n",
       "CUST_UID                                                   \n",
       "deca9c4409e84344bc4059116902ba0e                135692.0   \n",
       "25377f79cf9d4964a2f9893dc572caba                     NaN   \n",
       "66979a1614fb49f1bcdcdce75d6abefc                992125.6   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e               7356752.4   \n",
       "5e5817f15c8d4bdebefa820961854ac2                135002.0   \n",
       "\n",
       "                                  ICO_CUR_MON_ACM_TRX_AMT  \\\n",
       "CUST_UID                                                    \n",
       "deca9c4409e84344bc4059116902ba0e                      NaN   \n",
       "25377f79cf9d4964a2f9893dc572caba                      2.0   \n",
       "66979a1614fb49f1bcdcdce75d6abefc                      2.0   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e                8048976.0   \n",
       "5e5817f15c8d4bdebefa820961854ac2               81516420.9   \n",
       "\n",
       "                                  COUNTER_CUR_YEAR_CNT_AMT  \\\n",
       "CUST_UID                                                     \n",
       "deca9c4409e84344bc4059116902ba0e                       2.0   \n",
       "25377f79cf9d4964a2f9893dc572caba                       2.0   \n",
       "66979a1614fb49f1bcdcdce75d6abefc                       2.0   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e                       2.0   \n",
       "5e5817f15c8d4bdebefa820961854ac2                       2.0   \n",
       "\n",
       "                                  PUB_TO_PRV_TRX_AMT_CUR_YEAR  \\\n",
       "CUST_UID                                                        \n",
       "deca9c4409e84344bc4059116902ba0e                          2.0   \n",
       "25377f79cf9d4964a2f9893dc572caba                          2.0   \n",
       "66979a1614fb49f1bcdcdce75d6abefc                          2.0   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e                    2154106.4   \n",
       "5e5817f15c8d4bdebefa820961854ac2                    2421499.6   \n",
       "\n",
       "                                  MON_12_EXT_SAM_TRSF_IN_AMT  ...  \\\n",
       "CUST_UID                                                      ...   \n",
       "deca9c4409e84344bc4059116902ba0e                 594825002.0  ...   \n",
       "25377f79cf9d4964a2f9893dc572caba                         2.0  ...   \n",
       "66979a1614fb49f1bcdcdce75d6abefc                         2.0  ...   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e                         2.0  ...   \n",
       "5e5817f15c8d4bdebefa820961854ac2                         2.0  ...   \n",
       "\n",
       "                                  WTHR_OPN_ONL_ICO  EMP_NBR       REG_CPT  \\\n",
       "CUST_UID                                                                    \n",
       "deca9c4409e84344bc4059116902ba0e               NaN  26622.0           NaN   \n",
       "25377f79cf9d4964a2f9893dc572caba                 B   1502.0  3.800000e+08   \n",
       "66979a1614fb49f1bcdcdce75d6abefc                 A      2.0           NaN   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e                 B    432.0  5.000002e+06   \n",
       "5e5817f15c8d4bdebefa820961854ac2                 B    902.0  1.131689e+09   \n",
       "\n",
       "                                  SHH_BCK  HLD_DMS_CCY_ACT_NBR     REG_DT  \\\n",
       "CUST_UID                                                                    \n",
       "deca9c4409e84344bc4059116902ba0e      2.0                 12.0  242461.35   \n",
       "25377f79cf9d4964a2f9893dc572caba     32.0                 12.0     432.00   \n",
       "66979a1614fb49f1bcdcdce75d6abefc      2.0                 42.0        NaN   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e     12.0                 12.0     442.00   \n",
       "5e5817f15c8d4bdebefa820961854ac2     32.0                 12.0    3747.48   \n",
       "\n",
       "                                  LGP_HLD_CARD_LVL   OPN_TM  \\\n",
       "CUST_UID                                                      \n",
       "deca9c4409e84344bc4059116902ba0e               NaN  1588.13   \n",
       "25377f79cf9d4964a2f9893dc572caba               NaN    82.65   \n",
       "66979a1614fb49f1bcdcdce75d6abefc               NaN   512.65   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e                 C   432.00   \n",
       "5e5817f15c8d4bdebefa820961854ac2               NaN  3200.06   \n",
       "\n",
       "                                  NB_CTC_HLD_IDV_AIO_CARD_SITU  \\\n",
       "CUST_UID                                                         \n",
       "deca9c4409e84344bc4059116902ba0e                           NaN   \n",
       "25377f79cf9d4964a2f9893dc572caba                           NaN   \n",
       "66979a1614fb49f1bcdcdce75d6abefc                             C   \n",
       "84142dedb7e0442eb9aa7aeadf93f66e                             C   \n",
       "5e5817f15c8d4bdebefa820961854ac2                             C   \n",
       "\n",
       "                                  HLD_FGN_CCY_ACT_NBR  \n",
       "CUST_UID                                               \n",
       "deca9c4409e84344bc4059116902ba0e                  2.0  \n",
       "25377f79cf9d4964a2f9893dc572caba                  2.0  \n",
       "66979a1614fb49f1bcdcdce75d6abefc                 22.0  \n",
       "84142dedb7e0442eb9aa7aeadf93f66e                  2.0  \n",
       "5e5817f15c8d4bdebefa820961854ac2                  2.0  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from category_encoders.target_encoder import TargetEncoder\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.metrics import auc, roc_curve\n",
    "from lightgbm import LGBMRegressor\n",
    "\n",
    "\n",
    "# 1 导入数据\n",
    "train = pd.read_csv('./train.csv', index_col='CUST_UID')\n",
    "test = pd.read_csv('./test_A.csv', index_col='CUST_UID')\n",
    "# for data in (train,test):\n",
    "    \n",
    "target = train.pop('LABEL')\n",
    "# train=train.drop(['LGP_HLD_CARD_LVL'],axis=1)\n",
    "test = test[train.columns]\n",
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3e6e969",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2 非数值列\n",
    "s = train.apply(lambda x: x.dtype)\n",
    "tecols = s[s == 'object'].index.tolist()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "305f7289",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3 模型\n",
    "def makelgb():\n",
    "    lgbr = LGBMRegressor(colsample_bytree=1, learning_rate=0.04, max_depth=9,min_child_samples=200, min_child_weight=9, min_split_gain=0.25,n_estimators=600, num_leaves=40, reg_alpha=0.1, reg_lambda=0.1,scale_pos_weight=20, subsample=1, subsample_for_bin=5000)\n",
    "\n",
    "\n",
    "    return lgbr\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "494a3fb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4 k折交叉验证\n",
    "kf = KFold(n_splits=10, shuffle=True, random_state=100)\n",
    "devscore = []\n",
    "for tidx, didx in kf.split(train.index):\n",
    "    tf = train.iloc[tidx]  # 训练集\n",
    "    df = train.iloc[didx]  # 验证集\n",
    "    tt = target.iloc[tidx]  # 训练集target\n",
    "    dt = target.iloc[didx]  # 验证集target\n",
    "    te = TargetEncoder(cols=tecols)\n",
    "    tf = te.fit_transform(tf, tt)  # 训练集 目标编码器转换\n",
    "    df = te.transform(df)  # 验证集 目标编码器转换\n",
    "    lgbr = makelgb()\n",
    "    lgbr.fit(tf, tt)  # 训练集和训练集target 模型训练\n",
    "    pre = lgbr.predict(df)  # 验证集 预测的标签值\n",
    "    pre=(pre-pre.min())/(pre.max()-pre.min())\n",
    "    fpr, tpr, thresholds = roc_curve(dt, pre)  # 验证集预测结果和验证集target得出ROC\n",
    "    score = auc(fpr, tpr)\n",
    "    devscore.append(score)\n",
    "print(devscore)\n",
    "print(np.mean(devscore))\n",
    "#学习率 learning_rate=0.04\n",
    "# 0.01 0.942\n",
    "# 0.02 0.945 0.9471\n",
    "# 0.03 0.9454929749050904\n",
    "# 0.04 0.945793763040321\n",
    "# 0.05 0.945662671209727\n",
    "# 0.06 0.9456943771948717\n",
    "# 0.07 0.9448150133310993\n",
    "# 0.1 0.9441700480569988\n",
    "# colsample_bytree=0.6\n",
    "# 0.2 0.945793763040321\n",
    "# 0.3 0.9468828913765368\n",
    "# 0.4 0.946896249877111\n",
    "# 0.5 0.946896249877111\n",
    "# 0.6 0.9469567030071084\n",
    "# 0.7 0.9469019989659987\n",
    "# 0.8 0.9468748894447023\n",
    "# 0.9 0.9462398585772747\n",
    "# 1 0.9463836695026258\n",
    "#  max_depth=4\n",
    "# 3 \n",
    "# 4 0.9480801771249107\n",
    "# 5 0.947957063415975\n",
    "# 6 0.9476398803353654\n",
    "# 8 0.9471428727410227\n",
    "# 10  0.9466660810734655\n",
    "# num_leaves \n",
    "# 10 0.9479039863310348\n",
    "# 15 0.9477662330877115\n",
    "# 20 0.9480801771249107\n",
    "# 25 0.9480801771249107\n",
    "# 30 0.9480801771249107\n",
    "# 40 0.9480801771249107\n",
    "# 50 0.9480801771249107\n",
    "# 100  0.9480801771249107\n",
    "# 150\n",
    "# LGBMRegressor(learning_rate=0.01, max_depth=10, min_child_samples=200,n_estimators=1000, num_leaves=20, reg_alpha=0.1, reg_lambda=0.1,subsample_for_bin=5000) \n",
    "#0.948500635588687\n",
    "#LGBMRegressor(colsample_bytree=1, learning_rate=0.04, max_depth=9,min_child_samples=200, min_child_weight=9, min_split_gain=0.25,n_estimators=600, num_leaves=40, reg_alpha=0.1, reg_lambda=0.1,scale_pos_weight=20, subsample=1, subsample_for_bin=5000)\n",
    "#0.9485569297603135\n",
    "#LGBMRegressor(colsample_bytree=1, learning_rate=0.04, max_depth=9,min_child_weight=9, min_split_gain=0.25, n_estimators=600,num_leaves=40, scale_pos_weight=20, subsample=1)\n",
    "#0.9495004925427812\n",
    "# 不删 0.9495347185374952\n",
    "# 二分类、.946\n",
    "# 5折 0.9490168728242478"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "126d25c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5 在整个train集上重新训练，\n",
    "lgbr = makelgb()\n",
    "te = TargetEncoder(cols=tecols)  # 方法\n",
    "tf = te.fit_transform(train, target)  # fit_transform\n",
    "lgbr.fit(tf, target)  # 模型训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3904bf9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6 预测test\n",
    "df = te.transform(test)  # transform test数据集、目标编码器转换\n",
    "pre = lgbr.predict(df)\n",
    "pre=(pre-pre.min())/(pre.max()-pre.min())\n",
    "print(pre.min(),pre.max())\n",
    "results=pd.DataFrame({\n",
    "    'id':test.index.tolist(),\n",
    "    'pre': pre\n",
    "})\n",
    "# # 7 输出结果\n",
    "results.to_csv('./submit.txt',sep='\\t',header=None,index=False)\n",
    "print('输出成功！')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
